from telnetlib import EC

from selenium.webdriver.common.by import By
import time

from selenium.webdriver.support.wait import WebDriverWait

from common.Util import BlogDriver

#登录
class BlogLogin:
    driver=""
    url=""
    #构造函数
    def __init__(self):
        #Edge浏览器驱动对象
        self.driver=BlogDriver.driver
        # 博客系统网页
        self.url="http://8.137.19.140:9090/blog_login.html"
        # 进入登录页面
        self.driver.get(self.url)

    #成功登录博客页面的函数的函数
    def BlogSucText(self):
        # 找到用户名框输入，正确的账号
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        # 找到密码框输入，正确的密码
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        # 找到提交按钮，并且点击一下
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 成功登录后，查看一下用户昵称是否存在
        # 登录进去后来个登录成功的截图
        BlogDriver.GetScreeShot()
        # 登录进去后检查一下用户的昵称是否存在
        name=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3").text
        # 检查一下用户基本信息的文章是否存在
        article =self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)").text
        # 检查一下用户基本信息的分类是否存在
        classification =self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)").text
        # 打印name，article，classification
        print("BlogLogin: "+name)
        print("BlogLogin: "+article)
        print("BlogLogin: "+classification)

    # 错误登录博客的函数
    def BlogFailText(self):
        # 因为上面已经登录了，使用回退按钮，退回到登录页面
        self.driver.back() # 让浏览器后退，返回上一个页面
        # 找到用户命名输入框和密码输入框把里面的内容清空
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        time.sleep(1)
        # 然后在输入错误的账号或者错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # 截图错误账号和密码的图片
        BlogDriver.GetScreeShot()
        # 提交
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(2)
        # 警告弹窗
        alert = self.driver.switch_to.alert
        assert alert.text=="密码错误"
        print(alert.text)
        # 点击同意按钮
        alert.accept()




